package cqie.cn.frame.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import cqie.cn.frame.entity.SysUser;
import cqie.cn.frame.service.ISysUserService;
import cqie.cn.frame.utils.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.sql.ResultSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 王林
 * @version 1.0
 * @date 2021-09-10 周五
 * @time 14:38
 * @description
 */
@Slf4j
@Api(value = "登录公共接口", tags = "01系统管理--公共模块")
@ApiModel(value = "登录模块")
@RestController
@RequestMapping("/api/frame/common")
public class LoginController {
    @Resource
    private ISysUserService userService;

    //登录
    @ApiOperation(value = "登录", notes = "")
    @PostMapping(value = "/login1")
    public RetJson login(@RequestBody SysUser mo) {
        try {
            QueryWrapper<SysUser> wrapper = new QueryWrapper<>();
            wrapper.eq("account", mo.getAccount());
            List<SysUser> userList = userService.list(wrapper);
            //验证账号
            if (userList.size() == 0) {
                return RetJsonUtils.error("账号错误");
            }
            String token = null;
            boolean passwordRight = false;
            boolean statusRight = false;
            for (SysUser sysUser : userList) {
                if (sysUser.getPassword().equals(StringHandler.EncodeMD5(mo.getPassword()))) {
                    passwordRight = true;
                    if (sysUser.getStatus() == 0) {
                        statusRight = true;
                        //采用 session 保存用户，根据上下文获取session
//                        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
//                        HttpSession session = request.getSession();
//                        //  若存在，则修改；若不存在，则存入
//                        session.setAttribute("loginUser", sysUser);

                        // 生成JWT 令牌
//                        Map<String, String> payload = new HashMap<>();
//                        payload.put("account", mo.getAccount());
//                        payload.put("password", mo.getPassword());

                        // 生成JWT 令牌
                        log.info("username:{},password:{}", mo.getAccount(), mo.getPassword());


//                        token = JWTUtils.getToken(payload);
                        break;
                    }
                }
            }
            if (passwordRight == false) {
                return RetJsonUtils.error("登录密码错误");
            }
            if (statusRight == false) {
                return RetJsonUtils.error("用户已禁用");
            }
            return RetJsonUtils.success("登录成功", token);
        } catch (Exception e) {
            return RetJsonUtils.error(e.getMessage());
        }
    }

    //登录
    @ApiOperation(value = "登录22", notes = "")
    @GetMapping(value = "/login22")
//    @RequestMapping("/login")
    public ResponseEntity<Map<String, String>> login(String username, String password) {
        log.info("username:{},password:{}", username, password);
        Map<String, String> map = new HashMap<>();
        if (!"tom".equals(username) || !"123".equals(password)) {
            map.put("msg", "用户名密码错误");
            return ResponseEntity.ok(map);
        }
        JwtUtil jwtUtil = new JwtUtil();
        Map<String, Object> chaim = new HashMap<>();
        chaim.put("username", username);
        String jwtToken = jwtUtil.encode(username, 5 * 60 * 1000, chaim);
        map.put("msg", "登录成功");
        map.put("token", jwtToken);
        return ResponseEntity.ok(map);
    }

    @GetMapping("/testdemo")
    public ResponseEntity<String> testdemo() {
        return ResponseEntity.ok("我爱蛋炒饭");
    }


    @ApiOperation(value = "登录44444", notes = "")
    @PostMapping(value = "/login4444")
//    @(module="用户信息",style="查询",description="账号密码 随机数登录")
    public RetJson login4444(@RequestBody SysUser mo) {
        try {
            QueryWrapper<SysUser> wrapper = new QueryWrapper<>();
            wrapper.eq("account", mo.getAccount());
            List<SysUser> userList = userService.list(wrapper);
            //验证账号
            if (userList.size() == 0) {
                return RetJsonUtils.error("账号错误");
            }
            String jwtToken = null;
            boolean passwordRight = false;
            boolean statusRight = false;
            for (SysUser sysUser : userList) {
                if (sysUser.getPassword().equals(StringHandler.EncodeMD5(mo.getPassword()))) {
                    passwordRight = true;
                    if (sysUser.getStatus() == 0) {
                        statusRight = true;
                        //采用 session 保存用户，根据上下文获取session
//                        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
//                        HttpSession session = request.getSession();
//                        //  若存在，则修改；若不存在，则存入
//                        session.setAttribute("loginUser", sysUser);

                        // 生成JWT 令牌
                        log.info("username:{},password:{}", mo.getAccount(), mo.getPassword());
                        JwtUtil jwtUtil = new JwtUtil();
                        Map<String, Object> chaim = new HashMap<>();
                        chaim.put("account", mo.getAccount());
                        chaim.put("password", mo.getPassword());
                        jwtToken = jwtUtil.encode(mo.getAccount(), 5 * 60 * 1000, chaim);
                        break;
                    }
                }
            }
            if (passwordRight == false) {
                return RetJsonUtils.error("登录密码错误");
            }
            if (statusRight == false) {
                return RetJsonUtils.error("用户已禁用");
            }
            return RetJsonUtils.success("登录成功-后端1", jwtToken);
        } catch (Exception e) {
            return RetJsonUtils.error(e.getMessage());
        }
    }

}
